<!-- #INCLUDE FILE="inc/initDb.inc" -->
<!-- #INCLUDE FILE="inc/Common.asp" -->
<%
    Dim page_no, SortOrder, sqlParam, SearchCriteria, workLocation
    SearchCriteria = "All Jobs"
    '------------------------------------------------------------------------------------------------------------------------
    'Init Parameters from the form ------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------------------------------
    page_no = 1
    SortOrder = ""
    
    '------------------------------------------------------------------------------------------------------------------------
    'Create the SQL Statements ----------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------------------------------
    sql = "SELECT * FROM JOB_ADVERTISEMENT WHERE 1=1 AND BRANCH <> 'JP' "
    Set cmd = Server.CreateObject("ADODB.Command")

    sql = sql & " AND CURRENT_TIMESTAMP >= POSTING_DATE"
    sql = sql & " AND CLOSING_DATE >= POSTING_DATE"
    sql = sql & " AND CLOSING_DATE >= CURRENT_TIMESTAMP"

    Dim Ordering
    Ordering = "D"
    
    If Request.Item("Sort") <> "" Then
        SortOrder = Request.Item("Sort")
        Dim RealSort
        
        If Request.Item("Ordering") <> "" Then
            RealSort = " Desc"
        End If
        
        If SortOrder = "title" Then
            sql = sql & " ORDER BY JOB_ADV_TITLE " & RealSort
        Else 
            sql = sql & " ORDER BY POSTING_DATE " & RealSort
        End If
        'Do some checking here to prevent SQL injection
        orderBy = SortOrder
    Else
        sql = sql & " ORDER BY POSTING_DATE DESC"
        
    End If
    '------------------------------------------------------------------------------------------------------------------------
    'Executing the SQL Statements using params (?) to prevent SQL Injection -------------------------------------------------
    '------------------------------------------------------------------------------------------------------------------------
    SqlConn.Open
    cmd.ActiveConnection = SqlConn
    cmd.CommandText = sql

    Set rs = Server.CreateObject("ADODB.RecordSet")
    rs.Open cmd, , 3

    '------------------------------------------------------------------------------------------------------------------------
    'This pagination part MUST be after the rs.Open function. Else, it will throw an error that the Object is closed --------
    '------------------------------------------------------------------------------------------------------------------------
    If Request.Item("page") <> "" Then
        page_no = cint(Request.Item("page"))
        If page_no < 0 Then
            page_no = 1
        End If
    End If
    
    rs.PageSize = 20

    If page_no > rs.PageCount Then
        page_no = rs.PageCount
    End If
    
    if rs.PageCount > 0 Then
        rs.AbsolutePage = page_no
    End If
    
    
    'For displaying purposes
    Dim MinView, MaxView, RowIndex
    MinView = ((page_no -1) * rs.PageSize) + 1
    MaxView = MinView + rs.PageSize - 1
    RowIndex = MinView
    
    If MaxView > rs.RecordCount Then 
        MaxView = rs.RecordCount
    End If
    
    Dim NextScript, PrevScript
    If (page_no+1) > rs.PageCount Then
        NextScript = "javascript:void(0);"
    Else
        NextScript = "javascript:GoToPage('" & page_no + 1 & "', '" & orderBy & "');"
    End If
    
    If page_no =< 1 Then
        PrevScript = "javascript:void(0);"
    Else
        PrevScript = "javascript:GoToPage('" & page_no - 1 & "', '" & orderBy & "');"
    End If
%>
<div class="header">
    <h3>
        Results for "<%=SearchCriteria %>"
    </h3>
    <span class="subTitle">Your search returned
        <%=rs.RecordCount %>
        results</span>
    <div class="sortBy">
        <h3>
            Sort results by</h3>
        <ul>
            <li class="selected"><a href="#" onclick="javascript:GoToPage('<%=page_no %>', 'title');">
                Job Title</a></li>
            <li><a href="#" onclick="javascript:GoToPage('<%=page_no %>', 'datePosted');">Date posted</a></li>
        </ul>
    </div>
</div>
<div class="pagination">
    <% If rs.RecordCount > 0 Then %>
    <span class="results">Results <strong>
        <%=MinView %>
        -
        <%=MaxView %>
    </strong>of
        <%=rs.RecordCount    %>
    </span>
    <% Else %>
    <span class="results">Results <strong>0</strong> of 0</span>
    <% End If %>
    <ul>
        <li><a class="firstPage" title="First Page" href="#" onclick="javascript:GoToPage('1', '<%= orderBy %>');">
            <span class="hidden">First page</span></a></li>
        <li><a class="previousPage" title="Previous Page" href="#" onclick="<%=PrevScript %>">
            Previous</a></li>
        <%If rs.pagecount <> 0 Then%>
        <%  for i = 1 to rs.pagecount %>
        <%      If i = page_no then %>
        <li class="selected"><a class="pageNum" title="Page <%= i %>" href="javascript:void();">
            <%= i %>
        </a></li>
        <%      Else %>
        <li><a class="pageNum" title="Page <%= i %>" href="#" onclick="javascript:GoToPage('<%= i %>', '<%= orderBy %>');">
            <%= i %>
        </a></li>
        <%      End If %>
        <%  Next %>
        <%Else %>
        <li class="selected"><a class="pageNum" title="Page 1" href="javascript:void();">1</a></li>
        <%End If %>
        <%
                    
        %>
        <li><a class="nextPage" title="Next Page" href="#" onclick="<%=NextScript %>">Next</a></li>
        <li><a class="lastPage" title="Last Page" href="#" onclick="javascript:GoToPage('<%= rs.pagecount %>', '<%= orderBy %>');">
            <span class="hidden">Last page</span></a></li>
    </ul>
</div>
<!-- records container -->
<div class="recordsContainer">
    <div class="cTable">
        <p>
            <table width="100%" cellpadding="0" cellspacing="1" class="profileSubTable">
                <tr class="header">
                    <td style="width: 4%">
                        S/N</td>
                    <td>
                        Job Title</td>
                    <td style="width: 23%;">
                        Job Category</td>
                    <td style="width: 13%;">
                        Job Location</td>
                    <td style="width: 15%;">
                        Salary</td>
                    <td style="width: 13%;">
                        Post Date</td>
                </tr>
                <%If rs.pagecount <> 0 Then%>
                <%
	                    if not rs.eof then
	                        for i = MinView to MaxView
                %>
                <tr>
                    <td>
                        <%= RowIndex%>
                        .
                    </td>
                    <td>
                        <a href="Candidates_JobDetails.asp?jobID=<%=rs("JOB_ADV_CODE") %>">
                            <%=rs("JOB_ADV_TITLE")%>
                        </a>
                    </td>
                    <td>
                        <%=rs("JOB_CATEGORY")%>
                    </td>
                    <td>
                        <%=rs("WORK_LOCATION")%>
                    </td>
                    <td>
                        <%=rs("SALARY_OFFERED")%>
                    </td>
                    <td>
                        <%=FormatDate(rs("POSTING_DATE"))%>
                    </td>
                </tr>
                <%      
                                RowIndex =RowIndex + 1
                                rs.movenext
                                if rs.eof then exit for
                            next
                        end if
                %>
                <%Else %>
                <tr>
                    <td colspan="5">
                        No Results to display</td>
                </tr>
                <%End If %>
            </table>
        </p>
    </div>
</div>
<div class="pagination">
    <% If rs.RecordCount > 0 Then %>
    <span class="results">Results <strong>
        <%=MinView %>
        -
        <%=MaxView %>
    </strong>of
        <%=rs.RecordCount %>
    </span>
    <% Else %>
    <span class="results">Results <strong>0</strong> of 0</span>
    <% End If %>
    <ul>
        <li><a class="firstPage" title="First Page" href="#" onclick="javascript:GoToPage('1', '<%= orderBy %>');">
            <span class="hidden">First page</span></a></li>
        <li><a class="previousPage" title="Previous Page" href="#" onclick="<%=PrevScript %>">
            Previous</a></li>
        <%If rs.pagecount <> 0 Then%>
        <%'Show Pagination%>
        <% for i = 1 to rs.pagecount %>
        <% If i = page_no then %>
        <li class="selected"><a class="pageNum" title="Page <%= i %>" href="javascript:void();">
            <%= i %>
        </a></li>
        <% Else %>
        <li><a class="pageNum" title="Page <%= i %>" href="#" onclick="javascript:GoToPage('<%= i %>', '<%= orderBy %>');">
            <%= i %>
        </a></li>
        <% End If %>
        <% Next %>
        <%Else %>
        <li class="selected"><a class="pageNum" title="Page 1" href="javascript:void();">1</a></li>
        <%End If %>
        <%
                    
        %>
        <li><a class="nextPage" title="Next Page" href="#" onclick="<%=NextScript %>">Next</a></li>
        <li><a class="lastPage" title="Last Page" href="#" onclick="javascript:GoToPage('<%= rs.pagecount %>', '<%= orderBy %>');">
            <span class="hidden">Last page</span></a></li>
    </ul>
</div>
